package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ljfiction.book.Dialog.LoadingDIalog;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.yanzhenjie.loading.dialog.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class UpdataEmailActivity extends BaseActivity {

    @BindView(R.id.updataqianming_back)
    ImageView updataqianmingBack;
    @BindView(R.id.updataqianmingok)
    TextView updataqianmingok;
    @BindView(R.id.updataqianmingtext)
    ContainsEmojiEditText updataqianmingtext;
    private NoBackgLodDialog mloding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_email);
        ButterKnife.bind(this);
        updataqianmingtext.setText(UserManageBean.userBean.getUserAutograph());
    }

    @OnClick({R.id.updataqianming_back, R.id.updataqianmingok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.updataqianming_back:
                goBack();
                break;
            case R.id.updataqianmingok:
                if (StringTool.isEmpty(updataqianmingtext.getText().toString())) {
                    showToast("签名内容不能为空");
                    return;
                }
                toQianming();
                break;
        }
    }

    private void toQianming() {
        OkHttpUtils.post(HttpManager.IP + "/person-updateInfo")

                .params("userAutograph", updataqianmingtext.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (mloding==null){
                            mloding=new NoBackgLodDialog(UpdataEmailActivity.this,true);
                        }
                        mloding.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (mloding!=null){
                            mloding.dismis();

                        }
                        try {
                            JSONObject js=new JSONObject(s);
                            if (js.getInt("returnCode")==200){
                                UserManageBean.userBean.setUserAutograph(updataqianmingtext.getText().toString());
                                goBack();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
